package com.guguguweb.conn;

import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;

public class HeartBeatRespHandler extends ChannelInboundHandlerAdapter {
    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
        NettyMessage message = (NettyMessage) msg;
        if (message.getHeader() != null && message.getHeader().getType() == MessageType.HEART_REQ){
            System.out.println("<-----收到心跳包" + message);
            NettyMessage heartBeat = buildHeartBeat();
            System.out.println("发送心跳包回复----->" + heartBeat);
            ctx.writeAndFlush(heartBeat);
        } else {
            System.out.println("<-----收到其他");
            ctx.fireChannelRead(msg);
        }
    }

    private NettyMessage buildHeartBeat(){
        NettyMessage message = new NettyMessage();
        Header header = new Header();
        header.setType(MessageType.HEART_RESP);
        message.setHeader(header);
        return message;
    }
}
